package com.boby.homecamera

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.media.MediaExtractor
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.view.WindowManager
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.boby.homecamera.db.DBManager
import com.boby.homecamera.service.MainService

import kotlin.concurrent.thread


class MainActivity : AppCompatActivity() {

    private val permissions: Array<String>

    init {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            permissions = arrayOf(
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
                Manifest.permission.FOREGROUND_SERVICE,
                Manifest.permission.FOREGROUND_SERVICE_CAMERA,
                Manifest.permission.FOREGROUND_SERVICE_MICROPHONE,
                Manifest.permission.POST_NOTIFICATIONS
            )
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            permissions = arrayOf(
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
                Manifest.permission.FOREGROUND_SERVICE
            )
        } else {
            permissions = arrayOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        setContentView(R.layout.activity_main)
        val bCameraDemo = findViewById<Button>(R.id.b_camera_demo)
        val bCameraDemo2 = findViewById<Button>(R.id.b_camera_demo2)
        val bStartServer = findViewById<Button>(R.id.b_startServer)
        val b_test_decoder = findViewById<Button>(R.id.b_test_decoder)

        bCameraDemo.setOnClickListener {
            if (!hasPermissions(this, *permissions)) {
                ActivityCompat.requestPermissions(this, permissions, 1)
            } else {
                startActivity(Intent(this, CameraDemoActivity::class.java))
            }
        }
        bCameraDemo2.setOnClickListener {
            if (!hasPermissions(this, *permissions)) {
                ActivityCompat.requestPermissions(this, permissions, 1)
            } else {
                startActivity(Intent(this, Camera2Activity::class.java))
            }
        }

        bStartServer.setOnClickListener {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.setData(Uri.parse("package:" + this.packageName))
                startActivityForResult(intent, 0)
                return@setOnClickListener
            }

            if (!hasPermissions(this, *permissions)) {
                ActivityCompat.requestPermissions(this, permissions, 1)
            } else {
                DBManager.instance.init()
                startServer()
            }
        }
        b_test_decoder.setOnClickListener {
            testDecoder()
        }

    }

    private fun testDecoder() {
        thread {


            val extractor = MediaExtractor()
            extractor.setDataSource("${Environment.getExternalStorageDirectory().absolutePath}/test_error.mp4")


        }

    }

    private fun hasPermissions(context: Context?, vararg permissions: String): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null) {
            for (permission in permissions) {
                if (ActivityCompat.checkSelfPermission(
                        context,
                        permission
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    return false
                }
            }
        }
        return true
    }

    private fun startServer() {
        val intent = Intent(this, MainService::class.java)
        startService(intent)
    }

}